package com.easyweb.session;

import com.easyweb.entity.*;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.ejb.EntityManagerImpl;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;
import org.jboss.seam.faces.FacesMessages;

@Name("kullaniciHome")
public class KullaniciHome extends EntityHome<Kullanici> {
 
	private static final long serialVersionUID = -713325706733669156L;
	@In(create = true)
	KullaniciHome kullaniciHome;
	@In(create = true)
	IlceHome ilceHome;
	@In(create = true)
	BankaSubeleriHome bankaSubeleriHome;
	@In(create = true)
	IllerHome illerHome;
	@In(create = true)
	YabanciDilHome yabanciDilHome;
	@In(create = true)
	SemtlerHome semtlerHome;
	
	@In
	private EntityManager entityManager;
	
	BayiAdres bayiAdres= new BayiAdres();
	private List<Semtler> semtlerList = new ArrayList<Semtler>();;
	private List<Ilce> ilcelerList = new ArrayList<Ilce>();
	private List<BankaSubeleri> bankaSubeleriList=new ArrayList<BankaSubeleri>();

	private KullaniciIsTecrubeleri currentKullaniciIstecrubeleri;
	
	private KullaniciResim kullaniciResmi;
	
	private KullaniciHesap kullaniciHesap;
	/**
	    * Password confirmation
	    */
	   private String password;
	   private String confirm;   
	   private boolean verified;
	 
	  public String doIK01(){

		  // Criteria c=  ((EntityManagerImpl) entityManager.getDelegate()).getSession().createCriteria(KullaniciHesap.class);
		 // c.add(Restrictions.isNotNull("iptalKodu"));
		      
  
		verified = (confirm != null && confirm.equals(password)); 
		  if (!verified)
	      {
	         FacesMessages.instance().addToControl("sifre", "#{messages.errPasswordConfirmError}");
	      return "false";
	      }
		  return "true";
		  
	  }

	  public String doIK02(){
		  return "true";
		  
	  }
	  
	  public String doIK03(){
		  return "true";
		  
	  }
	  public String doIK04(){
		  return "true";
		  
	  }
	  public String doIK05(){
		  return "true";
	  }
	  
	  public String doIK06(){
		  return "true";
	  }
	  
	  public String doIK07(){
		  return "true";
	  }
	  
	  public String doIK08(){
		  return "true";
	  }
	  public String doIK09(){
		  return "true";
	  }
 	  
	  public String doIK10(){
		  return "true";
	  }
	 	  public void kullaniciIsTecrubesiSil(){
		  
		  entityManager.remove(currentKullaniciIstecrubeleri);
		  entityManager.flush();
	  }
	  
	  public void kullaniciIsTecrubesiKaydet(){
		entityManager.persist(currentKullaniciIstecrubeleri); 
		entityManager.flush(); 
	  }
	  
	  public void bankaCodeListener(ActionEvent event) {
		 	instance.setIl((Iller) bayiAdres.comboBul("body:bankaField:bankaID").getValue());
		 	subeListesiDoldur();
			}
	  
	  public void subeListesiDoldur(){
		if (getInstance().getBanka()!=null && getInstance().getBanka().getId() instanceof Long)
		{
			bankaSubeleriList = entityManager.createNamedQuery(
			"bankaSubelerBankayaGore").setParameter("p1",
					getInstance().getBanka().getBankaKodu()).getResultList();
		}
		  
	  }
	   
	  public void IlCodeListener(ActionEvent event) {
	 	instance.setIl((Iller) bayiAdres.comboBul("body:iLField:IlID").getValue());
			ilceListesiDoldur();
		}
	
	
	public void IlceCodeListener(ActionEvent event) {
		instance.setIlce((Ilce)bayiAdres.comboBul("body:ilceField:IlceID")
				.getValue());
		semtlerListesiDoldur();
	}
  
	public void ilceListesiDoldur() {
		if (getInstance().getIl() != null && getInstance().getIl().getId() instanceof Long) {
			ilcelerList = entityManager.createNamedQuery(
					"ilcelerTumKayitlarIleGore").setParameter("p1",
							getInstance().getIl().getId()).getResultList();
		}
	}

	public void semtlerListesiDoldur() {
		if (instance.getIlce() != null
				&& instance.getIlce().getId() instanceof Long) {
			semtlerList = entityManager.createNamedQuery(
					"semtlerTumKayitlarIleGore").setParameter("p1",
					instance.getIlce().getId()).getResultList();
		}
	}
	
	
	public void setKullaniciKullaniciId(Integer id) {
		setId(id);
	}

	public Integer getKullaniciKullaniciId() {
		return (Integer) getId();
	}

	@Override
	protected Kullanici createInstance() {
		Kullanici kullanici = new Kullanici();
		return kullanici;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		Ilce ilceByIlceId = ilceHome.getDefinedInstance();
		if (ilceByIlceId != null) {
			getInstance().setIlce(ilceByIlceId);
		}
		BankaSubeleri bankaSubeleri = bankaSubeleriHome.getDefinedInstance();
		if (bankaSubeleri != null) {
			getInstance().setBankaSube(bankaSubeleri);
		}
		
		 
		
		YabanciDil yabanciDilByYabanciDil3Id = yabanciDilHome
				.getDefinedInstance();
		if (yabanciDilByYabanciDil3Id != null) {
			getInstance().setYabanciDil3(yabanciDilByYabanciDil3Id);
		}
		YabanciDil yabanciDilByYabanciDil2Id = yabanciDilHome
				.getDefinedInstance();
		if (yabanciDilByYabanciDil2Id != null) {
			getInstance().setYabanciDil2(yabanciDilByYabanciDil2Id);
		}
		Iller illerByIlId = illerHome.getDefinedInstance();
		if (illerByIlId != null) {
			getInstance().setIl(illerByIlId);
		}
	 
		YabanciDil yabanciDilByYabanciDil1Id = yabanciDilHome
				.getDefinedInstance();
		if (yabanciDilByYabanciDil1Id != null) {
			getInstance().setYabanciDil1(yabanciDilByYabanciDil1Id);
		}
		Semtler semtlerBySemtId = semtlerHome.getDefinedInstance();
		if (semtlerBySemtId != null) {
			getInstance().setSemt(semtlerBySemtId);
		}
	 	}

	public boolean isWired() {
		return true;
	}

	public Kullanici getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

	public String getPassword() {
		return password;
	}

	public String getConfirm() {
		return confirm;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}


	public boolean isVerified() {
		return verified;
	}


	public void setVerified(boolean verified) {
		this.verified = verified;
	}



	public List<Semtler> getSemtlerList() {
		return semtlerList;
	}



	public List<Ilce> getIlcelerList() {
		return ilcelerList;
	}



	public void setSemtlerList(List<Semtler> semtlerList) {
		this.semtlerList = semtlerList;
	}



	public void setIlcelerList(List<Ilce> ilcelerList) {
		this.ilcelerList = ilcelerList;
	}

	public List<BankaSubeleri> getBankaSubeleriList() {
		return bankaSubeleriList;
	}

	public void setBankaSubeleriList(List<BankaSubeleri> bankaSubeleriList) {
		this.bankaSubeleriList = bankaSubeleriList;
	}

	public KullaniciIsTecrubeleri getCurrentKullaniciIstecrubeleri() {
		return currentKullaniciIstecrubeleri;
	}

	public void setCurrentKullaniciIstecrubeleri(
			KullaniciIsTecrubeleri currentKullaniciIstecrubeleri) {
		this.currentKullaniciIstecrubeleri = currentKullaniciIstecrubeleri;
	}

	public KullaniciResim getKullaniciResmi() {
		return kullaniciResmi;
	}

	public void setKullaniciResmi(KullaniciResim kullaniciResmi) {
		this.kullaniciResmi = kullaniciResmi;
	}

	public KullaniciHesap getKullaniciHesap() {
		return kullaniciHesap;
	}

	public void setKullaniciHesap(KullaniciHesap kullaniciHesap) {
		this.kullaniciHesap = kullaniciHesap;
	}


	
  
}
